<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Tools\SynAdTask;
use App\Admin\Extensions\Tools\SynTask;
use App\Handlers\RedisHandler;
use App\Models\AdTask;
use App\Models\RateType;
use App\Models\Task;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Widgets\Table;
use Illuminate\Foundation\Auth\User;
use Illuminate\Support\Facades\Input;

class AdTaskController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('广告任务');
            $content->description('系统每5分钟自动同步数据，如需立即同步，请点击 同步数据');
//            $content->row(view('admin.lineChart'));
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('广告任务');
            $content->description('');

            $content->body($this->form('edit')->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('广告任务');
            $content->description('');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(AdTask::class, function (Grid $grid) {

            if (!request()->get('_sort')) $grid->model()->orderByDesc('id');
            $grid->id('ID')->sortable();
            $grid->column('title', '广告名');
            $grid->column('status', '状态')->sortable()->display(function ($value) {
                $statusColor = config('system.task.status_color')[$value];
                return "<span class=\"label label-$statusColor\">" . config('system.task.status')[$value] . "</span>";
            });
            $grid->column('type', '广告类型')->sortable()->display(function ($value) {
                return config('system.ad_task.type')[$value];
            });
            $grid->column('live_time', '显示时间（分钟）');
            $grid->column('link', '广告链接');
            $grid->column('target_read_amount', '目标阅读')->sortable();
            $grid->column('valid_read_amount', '当前阅读')->sortable();
            $grid->column('enable', '启用')->switch(config('system.switch.states'))->sortable();
            $grid->created_at('创建时间')->sortable();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('title', '广告名');
                $filter->like('link', '广告链接');
                $filter->equal('enable', '启用')->select([1 => '启用', 2 => '禁用']);
                $filter->equal('status', '状态')->select(config('system.task.status'));
                $filter->equal('type', '广告类型')->select(config('system.ad_task.type'));
                $filter->between('target_read_amount', '目标阅读数');
                $filter->between('valid_read_amount', '有效阅读数');
                $filter->between('created_at', '创建时间')->date();
            });

            $grid->tools(function ($tools) {
                $tools->append(new SynTask());
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($mode = 'create')
    {
        return Admin::form(AdTask::class, function (Form $form) use ($mode) {
            $form->display('id', 'ID');
            $form->text('title', '广告名');
            $form->select('type', '广告类型')->options(config('system.ad_task.type'))->default(AD_TASK_TEXT_AND_IMG_TYPE);
            $form->number('target_read_amount', '目标阅读数')->default(1000)->rules('integer|required');
            $form->textarea('content', '广告正文');
            $form->multipleImage('pictures', '广告图片')->removable();
            $form->textarea('urls', '图片URL')->help('一行一个,如果填写图片URL，将优先使用URL');
//            $form->display('current_read_amount','当前阅读数');
            $form->text('link', '广告链接');
            $form->number('live_time', '广告显示时间（分钟）')->default(15)->rules('integer|required');
            $form->select('status', '状态')->options(config('system.task.status'))->default(TASK_STATUS_RUNNING);
            $form->switch('enable', '启用')->default(1)->states(config('system.switch.states'));
            $form->display('created_at', '创建时间');
            if ($mode == 'edit') $form->display('valid_read_amount', '当前阅读数');
            $form->hidden('user_id');
            $form->saving(function (Form $form) {
                $form->user_id = Admin::user()->id;
            });
        });
    }

    public function sync()
    {
        RedisHandler::syncAdTasks();
        return ['status_code' => 200, 'message' => '同步成功'];
    }

    public function clear()
    {
        RedisHandler::clearAdTaskDeviceLog();
        return ['status_code' => 200, 'message' => '清空成功'];
    }
}
